<template>
  <div class="app-container">
    <el-card class="dis">
      <el-form :inline="true" :model="formInline" class="demo-form-inline formHeight">
        <el-form-item label="">
          <el-input v-model="formInline.keyword" size="mini" placeholder="关键字:任务标题" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" @click="search">查询</el-button>
          <el-button type="primary" size="mini" @click="added">新增</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 表单 -->
    <el-card class="mt20">
      <el-button style="float:left;margin-bottom:10px" type="primary" @click="exportExcelSelect" size="mini">导出</el-button>
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        size="mini"
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          prop="task_num"
          label="编号"
          align="center"
          width="170"
        />
        <el-table-column
          prop="site_name"
          label="站点"
          align="center"
          width="100"
        />
        <el-table-column
          prop="task_title"
          label="任务标题"
          align="center"
          width="130"
        />
        <el-table-column
          :show-overflow-tooltip="true"
          prop="task_content"
          label="任务内容"
          align="center"
          width="170"
        />
        <el-table-column
          prop="task_start"
          label="开始时间"
          align="center"
          width="145"
        />
        <el-table-column
          prop="task_end"
          label="结束时间"
          align="center"
          width="145"
        />
        <el-table-column
          prop="task_status"
          label="任务状态"
          align="center"
          width="70"
        >
          <template slot-scope="scope">
            <el-tag>{{ scope.row.status_name }}</el-tag>
            <!-- <el-tag v-if="scope.row.task_status==1" type="success">{{ scope.row.status_name }}</el-tag>
            <el-tag v-if="scope.row.task_status==2" type="info">{{ scope.row.status_name }}</el-tag>
            <el-tag v-if="scope.row.task_status==3" type="waring">{{ scope.row.status_name }}</el-tag>
            <el-tag v-if="scope.row.task_status==4" type="danger">{{ scope.row.status_name }}</el-tag> -->
          </template>
        </el-table-column>
        <el-table-column
          prop="user_name"
          label="创建人"
          align="center"
          width="70"
        />
        <el-table-column
          prop="created_at"
          label="创建时间"
          align="center"
            :show-overflow-tooltip='true'
          width="120"
        />
        <el-table-column

          prop="name"
          label="附件"
          align="center"
          width="60"
        >
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="addOrEdit( 2,scope.row.id)">查看</el-button>

          </template>
        </el-table-column>

        <el-table-column
          label="操作"
          align="center"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
               v-if="scope.row.is_work!==1"
              @click="generateList(scope.row.id)"
            >生成工单</el-button>
              <el-button
              size="mini"
              type="info"
               v-if="scope.row.is_work==1"
            >已转工单</el-button>
                <el-button
              size="mini"
              type="primary"
              @click="handleEdit(scope.$index, scope.row)"
            >详情</el-button>
            <el-button
              size="mini"
              type="primary"
              @click="handleDelete(scope.$index, scope.row)"
            >进度</el-button>
            <el-button
              size="mini"
              type="primary"
              @click="edites(scope.row.id)"
            >修改</el-button>
            <el-button
              size="mini"
              type="primary"
             @click="dels(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination style='float: right;' v-show='totals>0' :total='totals' :page.sync='currentPage' :limit.sync='pageSixe' @pagination='getMaintainList' />

    </el-card>
    <!-- 底部分页 -->
<!--    <div class="currentPage">-->
<!--      <el-pagination-->
<!--        :current-page="currentPage"-->
<!--        :page-sizes="[10, 20, 50, 100]"-->
<!--        :page-size="pageSixe"-->
<!--        layout="total, sizes, prev, pager, next, jumper"-->
<!--        :total="totals"-->
<!--        @size-change="handleSizeChange"-->
<!--        @current-change="handleCurrentChange"-->
<!--        @prev-click="handelPrevChange"-->
<!--        @next-click="handelNextChange"-->
<!--      />-->
<!--    </div>-->
    <!-- 新增、 -->

      <Add
        @refreshList="getMaintainList"
        :details="details"
        :titles="tits"
        ref="operationsExplanAdded"
      />
        <!-- 附件组件 -->
    <div v-if="detailFlag">
      <Dialogs
        :id="detailId"
        :dialog-visible="detailFlag"
        :title="detailTitle1"
        @close="close"
        @confirm="confirm"
      />
    </div>
    <!-- 详情弹框 -->
    <el-dialog v-dialogDrag :title="detailTitle" center :visible.sync="dialogTableVisible1" width="30%">
      <div slot="title" class="header-title">
        <i class="el-icon-edit" />
        <span class="seeTit">{{ detailTitle }}</span>
      </div>
      <div>
        <Detail :accids="accids" @details="detailClose()" />
      </div>
    </el-dialog>
    <!-- 进度弹框 -->
    <el-dialog v-dialogDrag :title="proTitle" center :visible.sync="dialogTableVisible2">
      <div slot="title" class="header-title">
        <i class="el-icon-edit" />
        <span class="seeTit">{{ proTitle }}</span>
      </div>
      <div>
        <Progress :pro-dtetail="proDtetail" :num="num" />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="dialogTableVisible2 = false">关闭</el-button>
        <!-- <el-button type="primary" @click="dialogVisible = false">确 定</el-button> -->
      </span>
    </el-dialog>
    <!-- 导出组件 -->
    <ExportFile
      ref="myChild"
      :export-excel-info="exportExcelInfo"
      :table-data="tableAllData"
      :export-excel-arry="exportExcelArry"
    />
     <el-button v-if="false" :plain="true" @click="open2">成功</el-button>
  </div>
</template>
<script>
import { repairtaskList, progre,toWork,delsPlan,taskDetail } from '@/api/repair'
import Dialogs from './components/dialogs.vue'
import Detail from './components/detail.vue'
import Progress from './components/progress.vue'
import ExportFile from '@/components/exportFile/index'
import Add from './components/add.vue'

export default {
  name: 'index',
  components: {
    // eslint-disable-next-line vue/no-unused-components
    Dialogs,
    // eslint-disable-next-line vue/no-unused-components
    Detail,
    Progress,
    Add,
    ExportFile
  },
  data() {
    return {
      details:'',
      tits:'',
      tableData: [],
      multipleSelection: [], // 列表选中的数据
       generateShow:0,
      currentPage: 1,
      pageSixe: 10,
      totals: 0,
      formInline: {
        offset: 0,
        limit: 10,
        keyword: ''
      },
      tableAllData: [], // 导出的数据
      // 导出配置数据
      exportExcelArry: [{
        prop: 'task_num',
        label: '编号',
        formatterFlag: false
      }, {
        prop: 'site_name',
        label: '站点',
        formatterFlag: false
      }, {
        prop: 'task_title',
        label: '任务标题',
        formatterFlag: false
      }, {
        prop: 'task_content',
        label: '任务内容',
        formatterFlag: false
      }, {
        prop: 'task_start',
        label: '开始时间',
        formatterFlag: false
      }, {
        prop: 'task_end',
        label: '结束时间',
        formatterFlag: false
      },
      {
        prop: 'task_status',
        label: '任务状态',
        formatterFlag: false
      }, {
        prop: 'work_num',
        label: '创建人',
        formatterFlag: false
      }, {
        prop: 'created_at',
        label: '创建时间',
        formatterFlag: false
      }],
      // 导出excel表格id及excel名称
      exportExcelInfo: {
        excelId: 'retask-table',
        excelName: '抢修任务.xlsx'
      },
      dialogTableVisible: false,
      dialogTableVisible1: false,
      dialogTableVisible2: false,
      seeTitle: '附件列表',
       detailFlag: false, // 附件弹窗标识
      detailId: 0, // 附件详情id
      detailTitle1: '新增', // 弹窗标识
      seeDetails: [],
      detailTitle: '详情',
      rowDtetail: [],
      proTitle: '进度',
      proDtetail: '',
      num: '',
      accids: ''
    }
  },

  created() {
    this.getMaintainList()
  },
  methods: {
        open2() {
        this.$message({
          message: '操作成功',
          type: 'success'
        });
      },
  added(row) {
         this.tits='新增'
      this.details={}
      //this.dialogVisible = true
      // this.detailFlags=true
      this.$refs.operationsExplanAdded.open();
    },
           // 编辑
  edites(num){
     this.tits='编辑'
    taskDetail({id:num}).then(res=>{
      this.details=res.data
      if(this.details.length!==0){
        console.log('hahahhahah')
      // this.detailFlags=true

    this.$refs.operationsExplanAdded.open();

      }

    })
  },
    // 删除
    dels(num){
       this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delsPlan({id:num}).then(res=>{
        //  this.getList()
        this.getMaintainList()

      })

          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });

    },
    // 获取抢修列表
    getMaintainList() {
      this.formInline.offset = this.currentPage
      this.formInline.limit = this.pageSixe
      repairtaskList(this.formInline).then(res => {
        if (res.code === 200) {
          this.tableData = res.data.plans
          this.tableData.forEach((item) => {
            if (item.status_name == '') {
              item.status_name = '已提交'
            }
          })
          this.totals = res.data.total
        }
      })
    },
    // 搜索
    search() {
      this.formInline.offset = this.currentPage
      this.formInline.limit = this.pageSixe
      repairtaskList(this.formInline).then(res => {
        if (res.code === 200) {
          this.tableData = res.data.plans
          this.tableData.forEach((item) => {
            if (item.status_name == '') {
              item.status_name = '已提交'
            }
          })
          this.totals = res.data.total
        }
      })
    },
     // 查看
     addOrEdit(num, id) {
       console.log('报修',num)
      if (num === 1) {
        this.detailTitle = '新增'
        this.detailFlag = true
      } else {
        this.detailTitle1 = '附件'
        this.detailId = id
        this.detailFlag = true
      }
    },
    detailClosed(a) {
      this.dialogTableVisible = a
    },
    // 生成工单
    generateList(a){
      let data={
        id:a,
        type:'4'
      }
      toWork(data).then(res=>{
        if(res.code==200){
          this.getMaintainList()
          this.open2()

        }
      })
    },
    // 详情
    handleEdit(a, b) {
      this.dialogTableVisible1 = true
      this.accids = b.id
    },
    detailClose(a) {
      this.dialogTableVisible1 = a
    },
    // 进度
    handleDelete(a, b) {
      this.dialogTableVisible2 = true
      const data = {
        task_num: b.task_num
      }
      progre(data).then(res => {
        this.proDtetail = res.data.list
        if (this.proDtetail.length == 0) {
          const arr1 = [
            {
              status_name: '已提交',
              created_at: b.created_at,
              user_id:b.user_name
            },
            {
              status_name: '已派单',
              created_at: ''
            },
            {
              status_name: '执行中',
              created_at: ''
            },
            {
              status_name: '已完成',
              created_at: ''
            },
            {
              status_name: '已确认',
              created_at: ''
            }
          ]
          this.proDtetail = arr1
          this.num = 1
        } else {
          const nums = this.proDtetail[this.proDtetail.length - 1].task_status
          this.proDtetail.unshift(
            {
              status_name: '已提交',
              created_at: b.created_at,
               user_id:b.user_name
            }
          )
          this.num = this.proDtetail.length
          if (nums == 1) {
            const arr2 = [
              {
                status_name: '执行中',
                created_at: ''
              },
              {
                status_name: '已完成',
                created_at: ''
              },
              {
                status_name: '已确认',
                created_at: ''
              }
            ]
            this.proDtetail = this.proDtetail.concat(arr2)
          }
          if (nums == 2) {
            const arr3 = [

              {
                status_name: '已完成',
                created_at: ''
              },
              {
                status_name: '已确认',
                created_at: ''
              }
            ]
            this.proDtetail = this.proDtetail.concat(arr3)
          }
          if (nums == 3) {
            const arr4 = [
              {
                status_name: '已确认',
                created_at: ''
              }
            ]
            this.proDtetail = this.proDtetail.concat(arr4)
          }
        }
      })
    },
    // 表单选中
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 导出
    exportExcelSelect() {
      if (this.multipleSelection.length === 0) {
        this.tableAllData = this.tableData
      } else {
        this.tableAllData = this.multipleSelection
      }
      // // 将选中项传给this. tableAllData
      // this.tableAllData = this.selectListArry
      // 需要延时调导出方法，为了等待数据初始化到列表中
      setTimeout(() => {
        this.$refs.myChild.exportExcel()
      }, 500)
    },
    exportExcel() {
      this.initData('export-excel')
      // 获取到表格数据的值并赋给this.tableAllData
      // 同样延时调导出方法，需在initData('export-excel')方法执行成功后调this.$refs.myChild.exportExcel();方法
    },
    handleSizeChange(val) {
      this.pageSixe = val
      this.getMaintainList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getMaintainList()
    },
    handelPrevChange(val) {
      this.currentPage = val
      this.getMaintainList()
    },
    handelNextChange(val) {
      this.currentPage = val
      this.getMaintainList()
    },
      // 弹窗返回
    confirm() {
      this.close()
      this.getList()
    },
         //  关闭弹窗组件
    close() {
      this.detailFlag = false
      this.detailId = ''
    },
  }
}
</script>
<style lang="scss" scoped>
.con{
  padding-left: 5px;
  padding-right: 7px;
  .top_sea{
    position: relative;
    display: flex;
    height: 35px;
    width: 400px;
    font-size: 12px;
    .seaIcon{
      position: absolute;
      top:13px;
      left:8px;
      z-index: 999;
    }
    ::v-deep.el-input__inner{
       padding-left: 25px;
    }
  }
  .fj{
    color: #409EFF;
  }
  .currentPage{
    // display: flex;
    position: fixed;
    bottom: 0;
    right: 0;
    // flex-direction: row-reverse;
    // margin-top: 20px;
  }
  .header-title{
    .seeTit{
      padding-left: 10px;
    }
  }
  ::v-deep.el-dialog__header{
    background-color: #263445;
    color: #fff;
  }
  .dialog-footer{
        text-align: right;

  }
  ::v-deep.el-dialog__footer{
    text-align: right;
  }
}
.formHeight{
  height: 40px;
}
</style>
